package cn.openea.perpartner.expert.web.service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.openea.perpartner.expert.rabbit.TimelineProducer;
import cn.openea.perpartner.expert.web.domain.dto.TimelineDto;
import cn.openea.perpartner.expert.web.domain.entity.ExpertJob;
import cn.openea.perpartner.expert.web.service.ISignupService;
import cn.openea.perpartner.expert.web.service.ITimelineService;
import cn.openea.perpartner.meily.web.entity.Todo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.TimerTask;

/**
 * @author Weird077
 * @description TODO
 * @date 2023-07-07 12:22
 */
@Slf4j
@Service
public class TimelineServiceImpl implements ITimelineService {
    @Value("${sso.url}")
    private String ssoUri;
    private static final String AVATAR = "avatar";
    private static final String NICKNAME = "nickName";
    @Resource
    private ISignupService signupService;
    @Resource
    private TimelineProducer timelineProducer;

    private static final String JOB_DETAIL_URL = "/pages_private_scene1/ExpertCooperate/jobInfo?taskNumber=";

    @Override
    public TimerTask publishJob(ExpertJob job) {
        return new TimerTask() {
            @Override
            public void run() {
                JSONObject user = getUserData(job.getCreateUser());
                TimelineDto dto = TimelineDto.builder()
                        .type("ticket")
                        .userId(job.getCreateUser())
                        .avatar(user.getString(AVATAR))
                        .nickName(user.getString(NICKNAME))
                        .ticketName(job.getSubject())
                        .ticketNum(job.getTaskNum())
                        .ticketDetail(job.getDetails())
                        .eventTitle("火热报名")
                        .eventUrl(JOB_DETAIL_URL.concat(job.getTaskNum()))
                        .eventDetail(job.getDetails().length() > 12 ? job.getDetails().substring(0, 12).concat("...") : job.getDetails())
                        .build();
                timelineProducer.execute(dto);
            }
        };
    }


    @Override
    public TimerTask partnerSignup(Todo todo) {
        return new TimerTask() {
            @Override
            public void run() {
                int signupNum = signupService.selectSignupCount(todo.getReqNumber());
                JSONObject user = getUserData(todo.getPrvUserId());
                TimelineDto dto = TimelineDto.builder()
                        .userId(todo.getPrvUserId())
                        .avatar(user.getString(AVATAR))
                        .nickName(user.getString(NICKNAME))
                        .ticketNum(todo.getReqNumber())
                        .eventTitle("已报名")
                        .eventUrl(JOB_DETAIL_URL.concat(todo.getReqNumber()))
                        .eventDetail("当前报名人数：" + (int)NumberUtil.add(signupNum,1) + "人")
                        .build();
                timelineProducer.execute(dto);
            }
        };
    }

    @Override
    public TimerTask userPreselect(TimelineDto dto) {
        return new TimerTask() {
            @Override
            public void run() {
                JSONObject user = getUserData(dto.getUserId());
                dto.setAvatar(user.getString(AVATAR));
                dto.setNickName(user.getString(NICKNAME));
                dto.setEventTitle("初选完毕");
                dto.setEventUrl(JOB_DETAIL_URL.concat(dto.getTicketNum()));
                dto.setEventDetail("点击查看初选结果");
                timelineProducer.execute(dto);
            }
        };
    }

    @Override
    public TimerTask partnerReselect(Todo todo) {
        return new TimerTask() {
            @Override
            public void run() {
                JSONObject user = getUserData(todo.getPrvUserId());
                int reselectNum = signupService.selectSignupCount(todo.getReqNumber());
                TimelineDto dto = TimelineDto.builder()
                        .userId(todo.getPrvUserId())
                        .avatar(user.getString(AVATAR))
                        .nickName(user.getString(NICKNAME))
                        .ticketNum(todo.getReqNumber())
                        .eventTitle("参与复选")
                        .eventUrl(JOB_DETAIL_URL.concat(todo.getReqNumber()))
                        .eventDetail("当前复选人数：" + reselectNum + "人")
                        .build();
                timelineProducer.execute(dto);
            }
        };
    }

    @Override
    public TimerTask userReselect(TimelineDto dto) {
        return new TimerTask() {
            @Override
            public void run() {
                JSONObject user = getUserData(dto.getUserId());
                dto.setAvatar(user.getString(AVATAR));
                dto.setNickName(user.getString(NICKNAME));
                dto.setEventTitle("复选完毕");
                dto.setEventUrl(JOB_DETAIL_URL.concat(dto.getTicketNum()));
                dto.setEventDetail("点击查看复选结果");
                timelineProducer.execute(dto);
            }
        };
    }

    @Override
    public TimerTask partnerConfirm(TimelineDto dto) {
        return new TimerTask() {
            @Override
            public void run() {
                JSONObject user = getUserData(dto.getUserId());
                dto.setAvatar(user.getString(AVATAR));
                dto.setNickName(user.getString(NICKNAME));
                dto.setEventTitle("参与项目");
                dto.setEventUrl(JOB_DETAIL_URL.concat(dto.getTicketNum()));
                timelineProducer.execute(dto);
            }
        };
    }

    @Override
    public TimerTask partnerSubmitResult(TimelineDto dto) {
        return new TimerTask() {
            @Override
            public void run() {
                JSONObject user = getUserData(dto.getUserId());
                dto.setAvatar(user.getString(AVATAR));
                dto.setNickName(user.getString(NICKNAME));
                timelineProducer.execute(dto);
            }
        };
    }

    @Override
    public TimerTask userVerifyResult(TimelineDto dto) {
        return new TimerTask() {
            @Override
            public void run() {
                JSONObject user = getUserData(dto.getUserId());
                dto.setAvatar(user.getString(AVATAR));
                dto.setNickName(user.getString(NICKNAME));
                timelineProducer.execute(dto);
            }
        };
    }

    @Override
    public TimerTask rejectResult(Todo todo) {
        return new TimerTask() {
            @Override
            public void run() {
                JSONObject user = getUserData(todo.getPrvUserId());
                TimelineDto dto = TimelineDto.builder()
                        .userId(todo.getPrvUserId())
                        .avatar(user.getString(AVATAR))
                        .nickName(user.getString(NICKNAME))
                        .ticketNum(todo.getReqNumber())
                        .eventTitle(todo.getNodeName())
                        .eventUrl(JOB_DETAIL_URL.concat(todo.getReqNumber()))
                        .eventDetail(todo.getNodeName().concat("次返修成果未通过"))
                        .build();
                timelineProducer.execute(dto);
            }
        };
    }

    @Override
    public TimerTask userEvaluate(TimelineDto dto) {
        return new TimerTask() {
            @Override
            public void run() {
                JSONObject user = getUserData(dto.getUserId());
                dto.setAvatar(user.getString(AVATAR));
                dto.setNickName(user.getString(NICKNAME));
                timelineProducer.execute(dto);
            }
        };
    }


    private JSONObject getUserData(String userId) {
        JSONObject wxObject = new JSONObject();
        String result = HttpUtil.get(ssoUri + "/user/cache/" + userId);
        if (StrUtil.isNotBlank(result)) {
            wxObject = JSON.parseObject(result);
        }
        return wxObject.getJSONObject("data");
    }
}
